<?php

// 应用公共文件

use think\facade\Config;
use think\facade\Cookie;
use think\facade\Db;
use app\model\UserModel;
use app\common\business\AdminUser;


/**
 * 获取当前用户
 * @todo 同AdminUser/Login
 * @return array default=0/null
 */
function get_curr_user()
{
    $username = Cookie::get('username');
    $password = Cookie::get('password');
    if ($username == null || $password == null) {
        return [
            'id'       => 0,
            'username' => null,
            'password' => null,
            'status'   => -1,
        ];
    }
    $result = AdminUser::checkUser($username, $password);
    if($result['errno'] == 0) {
        $user = new UserModel();
        $result = $user
                    ->username($username)
                    ->field('id, status')
                    ->find();
        return [
            'id'       => $result['id'],
            'username' => $username,
            'password' => $password,
            'status'   => $result['status'],
        ];
    } else {
        return [
            'id'       => 0,
            'username' => null,
            'password' => null,
            'status'   => -1,
        ];
    }
}

/**
 * 用户id转换为用户名
 * @program int $userId id
 * @return bool
 */
function user_id_to_name($userId)
{
    $user = new UserModel();
    $result = $user
                ->uid($userId)
                ->find();
    return $result['username'];
}

/**
 * 用户是否为管理员
 * @program int $userStatus status
 * @return bool
 */
function is_admin($userStatus)
{
    if ($userStatus == 10) {
        return true;
    }
    return false;
}

/**
 * 返回数组索引字符串
 * @program array $arr
 * @return bool
 */
function get_array_value($arr, $keys)
{
    $result = $arr;
    foreach ($keys as $key) {
        if (!isset($result[$key])) {
            return null;
        }
        $result = $result[$key];
    }
    return $result;
}
